<?php
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class Member extends \app\admin\controller\Admin{
    public $service;
    public $model;
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $member_group=Db::name("member_group")->order("group_id asc")->select()->toArray();
        foreach($member_group as $key=>$v){
            $member_group[$key]['count']=Db::name('member')->where("group_id",$v['group_id'])->count();
        }
        View::assign('all_count',Db::name('member')->count());
        View::assign('member_group',$member_group);
        $this->validate=new \app\admin\validate\MemberValidate();
    }


    /**
     * 分页列表
     * @return string
     */
    public function index(): string
    {
        $where = [];
        $phone = input('phone', '', 'trim');
        if($phone){
            $where[] = ['phone', 'like', '%'.$phone.'%'];
        }

        $data = Db::name('member')->where($where)->order('id desc')->paginate();
        View::assign('data', $data);
        $paginate = $data->render();
        View::assign('paginate',$paginate);
        return View::fetch();
    }
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $data['password'] = md5($data['password']);
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['group_id'] = 3;
            Db::name("member")->insert($data);
            $this->UserLogModel->add(Request()->action(),"member",$data['id'],$this->request->param());
            $this->success($data['id']);
        }else{

            return View::fetch();
        }
    }
    public function update(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            Db::name("member")->update($data);
            $this->UserLogModel->add(Request()->action(),"member",$data['id'],$this->request->param());
            $this->success($data['id']);
        }else{
            $id=Request::param('id',0,"intval");
            $r=Db::name("member")->find($id);
            View::assign("r",$r);
            return View::fetch();
        }
    }
    public function detail()
    {
        $id = input('id');
        $r = Db::name('member')->find($id);
        View::assign('r', $r);
        return View::fetch();
    }

    public function delete()
    {
        $id = input('id');
        Db::name('member')->where('id', $id)->delete();
        $this->success('删除成功');
    }



}
